# forms.py
# Contains newforms and formsets for creating and editing objects described in models.py

__version__ = "$Revision: 67 $"
# $Id: forms.py 67 2010-10-03 15:38:36Z AxisOfEntropy@gmail.com $

from django import forms
from cv.widgets import NullSelectDateWidget

# Custom widget definitions
class FiveBoxFiveWidget(forms.Textarea):
    def __init__(self, *args, **kwargs):
        attrs = kwargs.setdefault('attrs',{})
        if 'rows' not in attrs:
            attrs['rows'] = '5'
        super(FiveBoxFiveWidget, self).__init__(*args, **kwargs)

# Custom field definitions
class FiveBoxField(forms.CharField):
    widget = FiveBoxFiveWidget

class SelectDateField(forms.DateField):
    import datetime
    this_year = datetime.date.today().year
    years = range(this_year, this_year-100, -1)

    widget = NullSelectDateWidget(years=years)

# Custom form definitions
class FacebookForm(forms.Form):
    def as_fb_table(self):
        "Returns this form rendered in FBML and a table -- excluding the <form><table></table></form>."
        return self._html_output(u'<tr><th class="detached_label">%(label)s</th><td class="editorkit_row">%(errors)s%(field)s%(help_text)s</td><td class="right_padding"></td></tr>', u'<fb:error><fb:message>%s</fb:message></fb:error>', u'<br />', u'<br />%s', True)

class FacebookModelForm(forms.ModelForm):
    # def as_fb_editor(self):
        # "Returns this form rendered in FBML <fb:editor-custom>s -- excluding the <fb:editor></fb:editor>."
        # return self._html_output(u'<fb:editor-custom label="">%(label)s%(errors)s%(field)s%(help_text)s</fb:editor-custom>', u'<fb:error><fb:message>%s</fb:message></fb:error>', u'<br />', u'<br />%s', True)

    def as_fb_table(self):
        "Returns this form rendered in FBML and a table -- excluding the <form><table></table></form>."
        return self._html_output(u'<tr><th class="detached_label">%(label)s</th><td class="editorkit_row">%(errors)s%(field)s%(help_text)s</td><td class="right_padding"></td></tr></table>', u'<fb:error><fb:message>%s</fb:message></fb:error>', u' ', u'<br />%s', True)

    def as_fb_table_extra(self):
        "Returns this form rendered in FBML and a table -- excluding the <form><table></table></form>."
        return self._html_output(u'<tr><th class="detached_label">%(label)s</th><td class="editorkit_row">%(errors)s%(field)s%(help_text)s</td><td class="right_padding"></td></tr>', u'<fb:error><fb:message>%s</fb:message></fb:error>', u' ', u'<br />%s', True)


# Custom formset definitions
class FacebookBaseInlineFormset(forms.models.BaseInlineFormset):
    def as_fb_table(self):
        from django.utils.safestring import mark_safe
        "Returns this formset rendered as FBML <tr>s -- excluding the <table></table>."
        # XXX: there is no semantic division between forms here, there
        # probably should be. It might make sense to render each form as a
        # table row with each field as a td.
        
        # forms = u' <fb:editor-divider /> '.join([form.as_fb_table() for form in self.forms])
        form_fbml = []

        # Render each of the forms.
        for form in self.forms:
            if form in self.initial_forms:
                form_fbml.append(form.as_fb_table())
            elif form in self.extra_forms:
                # If this form is one of the extras, don't display it immediately.
                # form_fbml.append('<a href="#" clicktotoggle="%s_off,%s_on">Add Another</a><div id="%s_off"></div><div id="%s_on">%s</div>' % ('dis_id', 'dis_id', 'dis_id', 'dis_id', form.as_fb_table()))
                form_fbml.append(form.as_fb_table_extra())
                
        # Join all the rendered forms into one string.        
        forms_fbml = u' <fb:editor-divider /> '.join(form_fbml)

        return mark_safe(u'\n'.join([unicode(self.management_form), forms_fbml]))

# Model form definitions
class LearnerInformationForm(FacebookModelForm):
    class Meta:
        from cv.models import LearnerInformation
        model = LearnerInformation
        exclude = ('photo',)
        
class LearnerInformationBasicForm(FacebookModelForm):
    birthdate = SelectDateField(required=False)
    class Meta:
        from cv.models import LearnerInformation
        model = LearnerInformation
        fields = (
            'firstname',
            'lastname',
            'birthdate',
            'gender',
            'application',
            'locale',            
        )

class LearnerInformationAddressForm(FacebookModelForm):
    class Meta:
        from cv.models import LearnerInformation
        model = LearnerInformation
        fields = (
            'addressLine1',
            'addressLine2',
            'municipality',
            'postalCode',
            'country',
        )

class LearnerInformationContactForm(FacebookModelForm):
    class Meta:
        from cv.models import LearnerInformation
        model = LearnerInformation
        fields = (
            'email',
            'mobile',
            'telephone',
            'fax',
            # 'web', # future
        )

class LearnerInformationSkillsForm(FacebookModelForm):
    skill_social = \
    skill_organizational = \
    skill_technical = \
    skill_computer = \
    skill_artistic = \
    skill_other = \
    skill_driving = \
    FiveBoxField(required=False)
    class Meta:
        from cv.models import LearnerInformation
        model = LearnerInformation
        fields = (
            'skill_social',
            'skill_organizational',
            'skill_technical',
            'skill_computer',
            'skill_artistic',
            'skill_other',
            'skill_driving',
        )

class LearnerInformationAdditionalForm(FacebookModelForm):
    additional = annexes = FiveBoxField(required=False)
    class Meta:
        from cv.models import LearnerInformation
        model = LearnerInformation
        fields = (
            'additional',
            'annexes',
        )

class WorkExperienceForm(FacebookModelForm):
    period_from = SelectDateField(required=False)
    period_to = SelectDateField(required=False)
    activities = FiveBoxField(required=False)
    class Meta:
        from cv.models import WorkExperience
        model = WorkExperience
        
class EducationForm(FacebookModelForm):
    period_from = SelectDateField(required=False)
    period_to = SelectDateField(required=False)
    class Meta:
        from cv.models import Education
        model = Education
        
class LanguageForm(FacebookModelForm):
    class Meta:
        from cv.models import Language
        model = Language

class PhotoForm(FacebookModelForm):
    class Meta:
        from cv.models import Photo
        model = Photo